package eventbus.center;

import com.google.common.eventbus.AsyncEventBus;

import java.time.Instant;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @desc 事件总线工具类
 **/
public class AsyncEventBusCenter {
    private volatile static AsyncEventBus asyncEventBus;
    private static Executor executor = Executors.newFixedThreadPool(2);

    //双重锁单例模式
    private static AsyncEventBus getAsynEventBus(){
        if(asyncEventBus==null){
            synchronized (AsyncEventBus.class){
                if(asyncEventBus==null){
                    asyncEventBus = new AsyncEventBus(executor);
                }
            }
        }
        return asyncEventBus;
    }

    //异步方式发送事件
    public static void post(Object event){
        System.out.println(Instant.now() +"发布事件："+event+"，线程号为："+Thread.currentThread().getName());
        getAsynEventBus().post(event);
    }
    public static void register(Object object){
        getAsynEventBus().register(object);
    }
 
}